yaml json files and json-schema
Table of Content
VSCode has the ability to display autocomplete suggestions for JSON and YAML format out of the box.
It’s use JSON schema to do it
JSON Schema#
JSON Schema is a specification that allows you to describe the structure of a JSON document and validate documents against that schema.
VSCode manage json schema by set json.schemas in settings.json
settings.json
"json.schemas": [
{
"fileMatch": [
"demo.json"
],
"url": "file:<path>/demo.schema.json"
}
]
demo.json
{
"ip": "127.0.0.1",
"port": 2000
}
demo.schema.json
{
"type": "object",
"properties": {
"ip": {
"type": "string"
},
"port": {
"type": "integer",
"maximum": 10000,
"minimum": 1024
}
},
"required": [
"ip",
"port"
]
}
online json schema creator
Demos#
- More schema options
- array
- enum
- dependentRequired
- anyOf
- If-Then-Else
array
"protocol": {
"type":"array",
"items": {
"type": "string",
"enum": ["tcp", "udp"]
},
"minItems": 2,
"uniqueItems": true
}
enum
"protocol": {
"type":"string",
"enum": ["tcp", "udp"]
}
dependentRequired
{
"type": "object",
"properties": {
"ip": {
"type": "string"
},
"port": {
"type": "integer",
"maximum": 10000,
"minimum": 1024
},
"advance": {
"type":"boolean"
}
},
"required": [
"ip"
],
"dependentRequired": {
"advance": ["port"]
}
}

anyOf#
schema reference - data property can be: - string with max two char - positive numbers
anyOf
{
"type": "object",
"properties": {
"data": {
"anyOf": [
{
"type": "string",
"maxLength": 2
},
{
"type": "number",
"minimum": 0
}
]
}
}
}
YAML and json schema#
Using YAML red hat we can config yaml file to validate against json schema
settings#
"yaml.schemas": {
"<shema file location>.json": "<file useage by schema>.yaml",
},
Demo#
- Simple schema withou